using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Umdf.ComInterop;
using System.IO;

namespace Umdf.SideShow.Events
{
    /// <summary>
    /// This event is posted in response to Content Missing Event on the device.  The
    /// struct contains the parameters that identify which device the event came
    /// from, which application/endpoint it is for, and the content id of the
    /// requested content.
    /// </summary>
    [Guid(SideShowEvents.SIDESHOW_CONTENT_MISSING_EVENT)]
    public class ContentMissingEvent : SideShowEvent
    {
        private Guid _applicationId;
        private Guid _endpointId;
        private uint _contentId;

        public ContentMissingEvent(BinaryReader reader)
        {
            DeserializeObject(reader);
        }

        public ContentMissingEvent(Guid applicationId, Guid endpointId, uint contentId)
            : base()
        {
            _applicationId = applicationId;
            _endpointId = endpointId;
            _contentId = contentId;
        }

        public Guid ApplicationId
        {
            get { return _applicationId; }
        }

        public Guid EndpointId
        {
            get { return _endpointId; }
        }

        public uint ContentId
        {
            get { return _contentId; }
        }

        protected override void WriteEventData(Stream s)
        {
            BinaryWriter writer = new BinaryWriter(s);
            writer.Write((uint)40);
            writer.Write(_applicationId.ToByteArray());
            writer.Write(_endpointId.ToByteArray());
            writer.Write(_contentId);
        }

        protected internal override void DeserializeObject(BinaryReader reader)
        {
            base.DeserializeObject(reader);
            _applicationId = new Guid(reader.ReadBytes(16));
            _endpointId = new Guid(reader.ReadBytes(16));
            _contentId = reader.ReadUInt32();
        }

        protected internal override void SerializeObject(BinaryWriter writer)
        {
            base.SerializeObject(writer);
            writer.Write(_applicationId.ToByteArray());
            writer.Write(_endpointId.ToByteArray());
            writer.Write(_contentId);
        }
    }
}
